package com.dc.project.system.controller;

import com.dc.common.validated.InsertGroup;
import com.dc.common.validated.QueryPageGroup;
import com.dc.common.validated.UpdateGroup;
import com.dc.common.vo.ResultVo;
import com.dc.project.system.dto.SysUserDto;
import com.dc.project.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 系统用户表(SysUser)表控制层
 *
 * @author zhuangcy
 * @since 2021-06-01 14:31:08
 */
@RestController
@RequestMapping("sysUser")
@Api(tags = "系统用户表")
public class SysUserController {
    /**
     * 服务对象
     */
    @Autowired
    private SysUserService sysUserService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    @RequiresPermissions("system:sysUser:query")
    public ResultVo get(@PathVariable Serializable id) {
        return this.sysUserService.queryById(id);
    }

    /**
     * 分页查询系统用户表
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("分页查询系统用户表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", defaultValue = "10", required = true)
    })
    @RequiresPermissions("system:sysUser:query")
    public ResultVo listPage(@Validated(QueryPageGroup.class) SysUserDto dto) {
        return this.sysUserService.queryByPage(dto);
    }

    /**
     * 新增系统用户表
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增系统用户表")
    @RequiresPermissions("system:sysUser:add")
    public ResultVo add(@Validated(value = {InsertGroup.class}) @RequestBody SysUserDto dto) {
        return this.sysUserService.add(dto);
    }

    /**
     * 编辑系统用户表
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("编辑系统用户表")
    @RequiresPermissions("system:sysUser:edit")
    public ResultVo edit(@Validated(value = {UpdateGroup.class}) @RequestBody SysUserDto dto) {
        return this.sysUserService.edit(dto);
    }

    /**
     * 根据主键删除系统用户表
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据主键删除系统用户表")
    @RequiresPermissions("system:sysUser:delete")
    public ResultVo delete(@PathVariable Serializable id) {
        return this.sysUserService.delete(id);
    }

    /**
     * 根据主键重置系统用户密码
     *
     * @param userId
     * @return
     */
    @PutMapping("resetPwd/{userId}")
    @ApiOperation("根据主键重置系统用户密码")
    @RequiresPermissions("system:sysUser:edit")
    public ResultVo resetPwd(@PathVariable Integer userId) {
        return this.sysUserService.resetPwd(userId);
    }

    /**
     * 变更系统用户密码
     *
     * @param userId
     * @return
     */
    @PutMapping("changePwd")
    @ApiOperation("变更系统用户密码")
    @RequiresPermissions("system:sysUser:edit")
    public ResultVo changePwd(
            @NotNull(message = "用户id不能为空") @RequestParam("userId") Integer userId,
            @NotNull(message = "原始密码不能为空") @RequestParam("oldPwd") String oldPwd,
            @NotNull(message = "新密码不能为空") @RequestParam("newPwd") String newPwd,
            @NotNull(message = "确认密码不能为空") @RequestParam("checkPwd") String checkPwd) {
        return this.sysUserService.changePwd(userId, oldPwd, newPwd, checkPwd);
    }

}